//https://leetcode.cn/problems/minimum-size-subarray-sum/
package codeRandomThoughts.Test209长度最小的子数组;

public class Solution {

    public int minSubArrayLen(int target, int[] nums) {
        //滑动窗口法,其实也是双指针
        int end = 0;
        int begin = 0;
        int sum = 0;
        int subL = 0;
        int result = nums.length + 1;
        for (end = 0; end < nums.length; end++) {
            sum += nums[end];
            while (sum >= target) {
                subL = end - begin + 1;
                result = Math.min(result, subL);
                sum -= nums[begin];
                begin++;
            }
        }
        return result == nums.length + 1 ? 0 : result;
    }


    //暴力解法 会超时
    public int minSubArrayLenBrutal(int target, int[] nums) {
        int[] results = new int[nums.length];
        int min = nums.length + 1;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum = 0;
            for (int j = i + 1; j < nums.length + 1; j++) {
                sum += nums[j - 1];
                if (sum >= target) {
                    results[i] = j - i;
                    if (results[i] < min)
                        min = results[i];
                    break;
                }
            }
        }
        return min == nums.length + 1 ? 0 : min;
    }
}
